package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/userlist")
    public List<Map<String,Object>> userList(){
        String sql="select * from ssm";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }
    @GetMapping("/addUser")
    public int addTest() {
        String sql = "insert into ssm value(10010,'艾斯',2000)";
        int i = jdbcTemplate.update(sql);
        return i;
    }
    @GetMapping("/updateUser/{id}")
    public int updateUser(@PathVariable("id") int id){
        String sql="update ssm set name=?,money=? where id="+id ;
        Object[] objects=new Object[2];
        objects[0]="胖虎";
        objects[1]=100;
        int i = jdbcTemplate.update(sql,objects);
        return i;
    }
    @GetMapping("/delUser/{id}")
    public int delUser(@PathVariable("id") int id){
        String sql="delete from ssm where id=?";
        int i = jdbcTemplate.update(sql,id);
        return i;
    }

}
